package com.help10000.rms.ui.adapter

import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.amir.common.beans.DetailHeaderData
import com.amir.common.beans.SectionData
import com.amir.common.holder.CommonViewHolder
import com.amir.common.mvp.base.BaseRecyclerViewAdapter
import com.help10000.rms.R
import com.help10000.rms.common.OrderType
import com.help10000.rms.mvp.view.ICustomKeyValueItemView
import com.help10000.rms.ui.activitys.OrderActivity

class OrderDetailAdapter(
    context: Context?,
    dataList: List<SectionData>,
    headerData: DetailHeaderData,
    customItemView: ICustomKeyValueItemView
) :
    BaseRecyclerViewAdapter<SectionData>(context, dataList) {
    private var customItemView: ICustomKeyValueItemView = customItemView
    private var headerData = headerData
    private var ITEM_TYPE_HEADER = 1
    private var ITEM_TYPE_DATA = 2
    private var ITEM_TYPE_FOOTER = 3
    override fun getItemViewType(position: Int): Int {
        return if (position == 0) ITEM_TYPE_HEADER else if (position == itemCount - 1) ITEM_TYPE_FOOTER else ITEM_TYPE_DATA
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonViewHolder {
        var view: View?
        if (viewType == ITEM_TYPE_HEADER) {
            view = layoutInflater.inflate(R.layout.item_order_detail_header, parent, false)
        } else if (viewType == ITEM_TYPE_FOOTER) {
            view = layoutInflater.inflate(R.layout.item_order_detail_footer, parent, false)
        } else {
            view = layoutInflater.inflate(R.layout.item_order_item_detail, parent, false)
        }
        return CommonViewHolder(view)
    }

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
        var viewType = getItemViewType(position)
        if (viewType == ITEM_TYPE_FOOTER) {
            holder.setOnClickListener(R.id.btn_lookhis) {
                val intent = Intent(mContext, OrderActivity::class.java)
                intent.putExtra("tel", headerData.Tel)
                intent.putExtra("type", OrderType.SERVICE_HISTORY_ORDER.ordinal)
                mContext.startActivity(intent)
            }
        } else if (viewType == ITEM_TYPE_HEADER) {
            holder.setText(R.id.tv_orderNo, headerData.ID)
            holder.setText(R.id.tv_status, headerData.Status)
            holder.setText(R.id.tv_price, headerData.Charge)
        } else {
            var order = dataList[position - 1]
            holder.setText(R.id.tabTitle, text = "${order.Title}")
            holder.setViewVisibility(
                R.id.sectionLayout,
                if (order.Title.isEmpty()) View.GONE else View.VISIBLE
            )
            var recyclerView = holder.getView<RecyclerView>(R.id.itemsRecyclerView)
            recyclerView.apply {
                itemAnimator = null
                layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
                adapter = OrderChildItemAdapter(context, order.Items, customItemView)
            }
        }
    }

    override fun getItemCount(): Int {
        return dataList.size + 1 + 1//头和脚
    }

}
